#Provide tasks to load and delete sample user data.
require 'active_record'
require 'active_record/fixtures'

namespace :db do
  DATA_DIRECTORY = "#{Rails.root}/lib/tasks/sample_data"
  namespace :sample_data do
    TABLES = %w(users specs faqs)
    MIN_USER_ID = 1000
    # Starting user id for the sample data
    desc "Load sample data"
    task :load => :environment do |t|
      class_name = nil
      # Use nil to get Rails to figure out the class.
      TABLES.each do |table_name|
        fixture = ActiveRecord::Fixtures.new(ActiveRecord::Base.connection,
        table_name, class_name,
        File.join(DATA_DIRECTORY, table_name.to_s))
        #fixture.insert_fixture :=>this method don't exist in version 3.1.3 of Rails 
        puts "Loaded data from #{table_name}.yml"
      end
    end
    desc "Remove sample data"
    task :delete => :environment do |t|
      User.delete_all("id>= #{MIN_USER_ID}")
      Spec.delete_all("user_id >= #{MIN_USER_ID}")
      Faq.delete_all( "user_id >= #{MIN_USER_ID}")
    end
  end
end